import UnoCSS from 'unocss/vite'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
export default defineConfig({
	base: './',
	plugins: [
		vue(),
		UnoCSS(),
		AutoImport({
			resolvers: [ElementPlusResolver()],
		}),
		Components({
			resolvers: [ElementPlusResolver()],
		}),
	],
	resolve: {
		alias: {
			'@': path.resolve(__dirname, './src')
		}
	},
	build: {
		// 启用 gzip 压缩
		// compress: 'gzip',

		// 启用代码分割
		target: 'esnext',
		cssMinify: true,
		// 分块策略
		rollupOptions: {
			output: {
				chunkFileNames: 'js/[name]-[hash].js',
				entryFileNames: 'js/[name]-[hash].js',
				assetFileNames: '[ext]/[name]-[hash].[ext]',
				manualChunks: {
					'vue-vendor': [
						'vue',
						'vue-router',
						'pinia',
						'@vue/runtime-dom',
						'@vue/runtime-core',
						'@vue/reactivity',
						'@vue/shared'
					],
					'element-plus': ['element-plus'],
					'chart': [
						'vue-data-ui'
					],
					'utils': [
						'axios',
						'lodash-es',
						'@vueuse/core'
					]
				}
			}
		},

		// 分包大小警告限制(kb)
		chunkSizeWarningLimit: 1000,

		// 禁用生成source map来减少打包时间,如需调试可以改为true
		sourcemap: false,

		// 启用实验性质的代码压缩器，性能更好
		minify: 'esbuild',
		assetsInlineLimit: 4096
	},
	// 优化依赖预构建
	optimizeDeps: {
		// 强制预构建vue相关依赖
		include: [
			'vue',
			'vue-router',
			'pinia',
			'element-plus',
			'element-plus/es/locale/lang/zh-cn',
			'axios',
			'lodash-es',
			'@vueuse/core',
			'vue-data-ui'
		],
		// 排除不需要预构建的依赖
		exclude: ['@unocss/reset']
	},

	// esbuild配置
	esbuild: {
		jsxFactory: 'h',
		jsxFragment: 'Fragment',
		// 删除console和debugger
		drop: ['console', 'debugger']
	}
})
